{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "1_3.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YALwAycGUG6z",
        "colab_type": "text"
      },
      "source": [
        "### 第三课 机器学习构建chatbot"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aLjV1R1iVSR3",
        "colab_type": "text"
      },
      "source": [
        "* 主要内容\n",
        "  * 关于聊天机器人的思考\n",
        "    * 1.工程考量\n",
        "    * 2.机器学习角度考量\n",
        "   * 预备知识\n",
        "     * 1.检索与匹配\n",
        "     * 2.分类与朴素贝叶斯\n",
        "   * chatterbot\n",
        "     * 1.架构与使用方法\n",
        "     * 2.源码分析\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZQp-gELMVwCR",
        "colab_type": "text"
      },
      "source": [
        "### 传统聊天机器人\n",
        "* NLP基础知识\n",
        "  * 基本分词\n",
        "  * 关键词抽取(TF-IDF等)\n",
        "  * 正则表达式模式匹配\n",
        "  * ....\n",
        "* Machine Learning相关知识\n",
        "  * 文本表示与匹配\n",
        "  * 分类(文本场景分析)\n",
        "  * 数据驱动(特征工程)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4Xob5sUOWfKS",
        "colab_type": "text"
      },
      "source": [
        "### 检索与匹配\n",
        "* 基于检索与匹配\n",
        "  * 知识库(存储了问题与回复内容)\n",
        "  * 检索：搜寻相关问题\n",
        "  * 匹配：对结果进行排序"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rS6X2qflGuXq",
        "colab_type": "text"
      },
      "source": [
        "开放域机器人：\n",
        "* 什么都想插一脚\n",
        "\n",
        "Closet Match Adapter\n",
        "* 字符串模糊匹配（编辑距离）\n",
        "  * 编辑距离/Levenshtein距离，是指两个字符串之间，由一个转成另外一个所需要的最少编辑操作次数\n",
        "\n",
        "\n",
        "```\n",
        "# !pip install python-Levenshtein\n",
        "import Levenshtein\n",
        "texta=u'七月在线'\n",
        "textb=u'七天在线'\n",
        "Levenshtein.distance(texta,textb)\n",
        "```\n",
        "\n",
        "\n",
        "  * 计算出两个句子之间的置信度\n",
        "    * 用来评估你提出的问题和数据库中存在的问题的相似程度。如果自己提出的问题和数据库中存在的问题**编辑距离**很近，那么可以认为是同一个问题，这样就可以根据数据库中已有的问题反馈给用户数据库中已存在的答案\n",
        "\n",
        "\n",
        "* TF-IDF\n",
        "  * 如果使用上面提到的编辑距离，这时候会出现一个问题，因为在评估中每个词我们设定的权重是一样的，比如下面两句话，虽然编辑距离很相似，但是提问的不是一个问题，面对这种情况我们可以用TF-IDF进行根据不同的权重，提取出关键词，衡量一个词对整个句子的重要性，针对关键词来做比对，这样就可以区分出不是一个问题\n",
        "    * 你喜欢什么书\n",
        "    * 你喜欢什么电影\n",
        "\n",
        "* Closest Meaning Adapater\n",
        "  * 借助nltk的WordNet，近义词评估。如下面两个句子，从nltk巨大的近义词库中我们发现关键词是近义词，那么就可以认为这两句话表达含义是一样的\n",
        "    * 你喜欢这本书吗\n",
        "    * 你喜欢这本资料吗\n",
        "\n",
        "* Time Logic Adapter\n",
        "  * 处理涉及时间的提问。这一块范围就设置的比较具体了，主要回答针对时间的提问\n",
        "\n",
        "* Mathematical Evaluation Adapter\n",
        "  * 涉及数学运算。\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "BCNF82MBY7HL",
        "colab_type": "text"
      },
      "source": [
        "* 场景分类\n",
        "  * 朴素贝叶斯(NB)\n",
        "    * 我们输入一个词(篮球鞋)，会根据概率去判断它属于哪个类(运动鞋)\n",
        "    $$P(B \\mid A)=\\frac{P(A \\mid B) P(B)}{P(A)}$$"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OB76t6aMcfhK",
        "colab_type": "text"
      },
      "source": [
        "聊天机器人遇到的第一个问题就是用什么方式去回答这个问题，会涉及两个问题：\n",
        "* **匹配的时候用什么方式**\n",
        "* **不同的场景用什么方式**"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zLGirP_Le2P_",
        "colab_type": "text"
      },
      "source": [
        "### Chatterbot聊天机器人\n",
        "\n",
        "ChatterBot是一个基于机器学习的聊天机器人引擎，构建在python上，主要特点是可以从已有的对话中进行学习(记忆匹配)\n",
        "\n",
        "* 官网：https://chatterbot.readthedocs.io/en/stable/\n",
        "* Github:https://github.com/gunthercox/ChatterBot"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WJkc2Ld5fNHt",
        "colab_type": "text"
      },
      "source": [
        "每个部分都设计了不同的\"适配器\"(Adapter)\n",
        "* **机器人应答逻辑** => `Logic Adapters`\n",
        "  * Closest Match Adapter\n",
        "    * 字符串模糊匹配(**编辑距离**)\n",
        "  * Closest Meaning Adapter\n",
        "    * 借助nltk的WordNet，**近义词评估**\n",
        "  * Time Logic Adapter\n",
        "    * 处理涉及**时间**的提问\n",
        "  * Mathematical Evaluation Adapter\n",
        "    * 涉及**数学运算**"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Ip0vuw9pfhYT",
        "colab_type": "text"
      },
      "source": [
        "  * **存储器后端** => `Storage Adapters`\n",
        "    * Read Only Mode\n",
        "      * 只读模式，当有输入数据到chatterbot的时候，数\n",
        "据库并不会发生改变\n",
        "    * Json Database Adapter\n",
        "      * 用以存储对话数据的接口，对话数据以Json格式\n",
        "进行存储。线上不建议采取这种方法，需要缓存\n",
        "    * Mongo Database Adapter\n",
        "      * 以MongoDB database方式来存储对话数据"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7grOTWELgBep",
        "colab_type": "text"
      },
      "source": [
        "  * **输入形式** => `Input Adapters`\n",
        "    * Variable input type adapter\n",
        "      * 允许chatter bot接收不同类型的输入的，如\n",
        "strings,dictionaries和Statements\n",
        "    * Terminal adapter\n",
        "      * 使得ChatterBot可以通过终端进行对话\n",
        "    * HipChat Adapter\n",
        "      * 使得ChatterBot 可以从HipChat聊天室获取输入语\n",
        "句，通过HipChat 和 ChatterBot 进行对话\n",
        "    * Speech recognition\n",
        "      * 语音识别输入，详见chatterbot-voice "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fgeY5gHEgXBK",
        "colab_type": "text"
      },
      "source": [
        "  * **输出形式** => `Output Adapters`\n",
        "    * Output format adapter\n",
        "      * 支持text，json和object格式的输出\n",
        "    * Terminal adapter\n",
        "    * HipChat Adapter\n",
        "    * Mailgun adapter\n",
        "      * 允许chat bot基于Mailgun API进行邮件的发送\n",
        "    * Speech synthesis\n",
        "      * TTS(Text to speech)部分，详见chatterbot-voice "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "U1S4vr7HiwK1",
        "colab_type": "text"
      },
      "source": [
        "### 代码"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lAbljSkSi7X0",
        "colab_type": "text"
      },
      "source": [
        "chatterbot是一款python接口的，基于一系列规则和机器学习算法完成的聊天机器人。具有结构清晰，可扩展性好，简单实用的特点。"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9hASYCYbi_Qe",
        "colab_type": "text"
      },
      "source": [
        "##### 安装\n",
        "是的，安装超级简单，用pip就可以啦\n",
        "\n",
        " pip install chatterbot"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5oVZOQ6KjKjc",
        "colab_type": "text"
      },
      "source": [
        "#### 各式各样的Adapter\n",
        "\n",
        "大家已经知道chatterbot的聊天逻辑和输入输出以及存储，是由各种adapter来限定的，我们先看看流程图，一会再看例子，看看怎么用\n",
        "![](http://chatterbot.readthedocs.io/en/stable/_images/chatterbot-process-flow.svg)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "l-IJul1fjqYM",
        "colab_type": "text"
      },
      "source": [
        "### 基础版本"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "r8V_VL16crVI",
        "colab_type": "text"
      },
      "source": [
        "#### 基于编辑距离"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ChbJRGZJjsk9",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "outputId": "14b5b8fc-2ee6-4da6-ca13-02b0f08e6e23"
      },
      "source": [
        "# -*- coding: utf-8 -*-\n",
        "# !pip install chatterbot\n",
        "# https://github.com/gunthercox/ChatterBot/blob/master/examples/default_response_example.py\n",
        "from chatterbot import ChatBot\n",
        "from chatterbot.trainers import ListTrainer\n",
        "\n",
        "# 构建ChatBot并指定Adapter\n",
        "bot = ChatBot(\n",
        "    'Default Response Example Bot',\n",
        "    storage_adapter='chatterbot.storage.SQLStorageAdapter',#JsonFileStorageAdapter was removed.\n",
        "    # 基于编辑距离\n",
        "    logic_adapters=[\n",
        "        {\n",
        "            'import_path': 'chatterbot.logic.BestMatch',#编辑距离\n",
        "             'default_response': 'I am sorry, but I do not understand.',#阈值低于0.65返回值\n",
        "            # 'import_path': 'chatterbot.logic.LowConfidenceAdapter',  was removed\n",
        "            'maximum_similarity_threshold': 0.65,\n",
        "        }\n",
        "    ]\n",
        ")\n",
        "trainer=ListTrainer(bot)#ListTreainer表示列表，两个人对话\n",
        "\n",
        "# 手动给定一点语料用于训练\n",
        "trainer.train([\n",
        "    'How can I help you?',\n",
        "    'I want to create a chat bot',\n",
        "    'Have you read the documentation?',\n",
        "    'No, I have not',\n",
        "    'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html'\n",
        "])\n",
        "\n",
        "# 给定问题并取回结果\n",
        "question = 'How do I make an omelette?'\n",
        "print('question:',question)\n",
        "response = bot.get_response(question)\n",
        "print('ans:',response)\n",
        "\n",
        "print(\"\\n\")\n",
        "question = 'how to make a chat bot?'\n",
        "print('question:',question)\n",
        "response = bot.get_response(question)\n",
        "print('ans:',response)"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "List Trainer: [####################] 100%\n",
            "question: How do I make an omelette?\n",
            "ans: I am sorry, but I do not understand.\n",
            "\n",
            "\n",
            "question: how to make a chat bot?\n",
            "ans: I am sorry, but I do not understand.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UFetzHs-n_LT",
        "colab_type": "text"
      },
      "source": [
        "#### 处理时间和数学计算的Adapter"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OX2BNXd6wXEm",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        },
        "outputId": "60d22967-13fe-4908-c920-69f0ff96cdfe"
      },
      "source": [
        "# -*- coding: utf-8 -*-\n",
        "# https://github.com/gunthercox/ChatterBot\n",
        "from chatterbot import ChatBot\n",
        "\n",
        "\n",
        "bot = ChatBot(\n",
        "    \"Math & Time Bot\",\n",
        "    logic_adapters=[\n",
        "        \"chatterbot.logic.MathematicalEvaluation\",#处理时间\n",
        "        \"chatterbot.logic.TimeLogicAdapter\"#处理数学计算\n",
        "    ],\n",
        "    input_adapter=\"chatterbot.input.VariableInputTypeAdapter\",\n",
        "    output_adapter=\"chatterbot.output.OutputAdapter\"\n",
        ")\n",
        "\n",
        "# 进行数学计算\n",
        "question = \"What is 4 + 9?\"\n",
        "print(question)\n",
        "response = bot.get_response(question)\n",
        "print(response)\n",
        "\n",
        "print(\"\\n\")\n",
        "\n",
        "# 回答和时间相关的问题\n",
        "question = \"What time is it?\"\n",
        "print(question)\n",
        "response = bot.get_response(question)\n",
        "print(response)"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "What is 4 + 9?\n",
            "4 + 9 = 13\n",
            "\n",
            "\n",
            "What time is it?\n",
            "The current time is 10:25 AM\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fXt1amWFog28",
        "colab_type": "text"
      },
      "source": [
        "#### 导出语料到json文件"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vRuyGaW2L5Sh",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 340
        },
        "outputId": "fc9d89e4-beba-445e-d750-8570db789d28"
      },
      "source": [
        "# -*- coding: utf-8 -*-\n",
        "# https://github.com/zhangyuankai2018/chatterbot-corpus\n",
        "from chatterbot import ChatBot\n",
        "from chatterbot.trainers import ChatterBotCorpusTrainer\n",
        "'''\n",
        "如果一个已经训练好的chatbot，你想取出它的语料，用于别的chatbot构建，可以这么做\n",
        "'''\n",
        "\n",
        "chatbot = ChatBot(\n",
        "    'Export Example Bot',\n",
        ")\n",
        "#trainer='chatterbot.trainers.ChatterBotCorpusTrainer'\n",
        "trainer=ChatterBotCorpusTrainer(chatbot)\n",
        "\n",
        "# 训练一下咯\n",
        "trainer.train('/content/drive/My Drive/VQA/english')\n",
        "\n",
        "# 把语料导出到json文件中\n",
        "trainer.export_for_training('/content/drive/My Drive/VQA/my_export.json')"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Training ai.yml: [####################] 100%\n",
            "Training botprofile.yml: [####################] 100%\n",
            "Training computers.yml: [####################] 100%\n",
            "Training conversations.yml: [####################] 100%\n",
            "Training emotion.yml: [####################] 100%\n",
            "Training food.yml: [####################] 100%\n",
            "Training gossip.yml: [####################] 100%\n",
            "Training greetings.yml: [####################] 100%\n",
            "Training health.yml: [####################] 100%\n",
            "Training history.yml: [####################] 100%\n",
            "Training humor.yml: [####################] 100%\n",
            "Training literature.yml: [####################] 100%\n",
            "Training money.yml: [####################] 100%\n",
            "Training movies.yml: [####################] 100%\n",
            "Training politics.yml: [####################] 100%\n",
            "Training psychology.yml: [####################] 100%\n",
            "Training science.yml: [####################] 100%\n",
            "Training sports.yml: [####################] 100%\n",
            "Training trivia.yml: [####################] 100%\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "TogYBM8Wz1Ox",
        "colab_type": "text"
      },
      "source": [
        "#### 使用Ubuntu数据集构建聊天机器人"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AX2AK8ujHMwt",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from chatterbot import ChatBot\n",
        "import logging\n",
        "from chatterbot.trainers import UbuntuCorpusTrainer\n",
        "\n",
        "'''\n",
        "这是一个使用Ubuntu语料构建聊天机器人的例子\n",
        "'''\n",
        "\n",
        "# 允许打日志\n",
        "logging.basicConfig(level=logging.INFO)\n",
        "\n",
        "chatbot = ChatBot(\n",
        "    'Example Bot',\n",
        ")\n",
        "trainer=UbuntuCorpusTrainer(chatbot)\n",
        "\n",
        "# 使用Ubuntu数据集开始训练\n",
        "trainer.train()\n",
        "\n",
        "# 我们来看看训练后的机器人的应答\n",
        "response = chatbot.get_response('How are you doing today?')\n",
        "print(response)"
      ],
      "execution_count": 16,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OvFAzVBz77Zy",
        "colab_type": "text"
      },
      "source": [
        "#### 借助微软的聊天机器人"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qzDpR3dr7-Cv",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# -*- coding: utf-8 -*-\n",
        "from chatterbot import ChatBot\n",
        "from settings import Microsoft\n",
        "\n",
        "'''\n",
        "关于获取微软的user access token请参考以下的文档\n",
        "https://docs.botframework.com/en-us/restapi/directline/\n",
        "'''\n",
        "\n",
        "chatbot = ChatBot(\n",
        "    'MicrosoftBot',\n",
        "    directline_host = Microsoft['directline_host'],\n",
        "    direct_line_token_or_secret = Microsoft['direct_line_token_or_secret'],\n",
        "    conversation_id = Microsoft['conversation_id'],\n",
        "    input_adapter='chatterbot.input.Microsoft',\n",
        "    output_adapter='chatterbot.output.Microsoft',\n",
        "    trainer='chatterbot.trainers.ChatterBotCorpusTrainer'\n",
        ")\n",
        "\n",
        "chatbot.train('/content/sample_data/english')\n",
        "\n",
        "# 是的，会一直聊下去\n",
        "while True:\n",
        "    try:\n",
        "        response = chatbot.get_response(None)\n",
        "\n",
        "    # 直到按ctrl-c 或者 ctrl-d 才会退出\n",
        "    except (KeyboardInterrupt, EOFError, SystemExit):\n",
        "        break "
      ],
      "execution_count": 18,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "r3D8eA5k8Wx2",
        "colab_type": "text"
      },
      "source": [
        "#### HipChat聊天室Adapter"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PO-AMg1EHM3m",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# -*- coding: utf-8 -*-\n",
        "from chatterbot import ChatBot\n",
        "from settings import HIPCHAT\n",
        "\n",
        "'''\n",
        "炫酷一点，你可以接到一个HipChat聊天室，你需要一个user token，下面文档会告诉你怎么做\n",
        "https://developer.atlassian.com/hipchat/guide/hipchat-rest-api/api-access-tokens\n",
        "'''\n",
        "\n",
        "chatbot = ChatBot(\n",
        "    'HipChatBot',\n",
        "    hipchat_host=HIPCHAT['HOST'],\n",
        "    hipchat_room=HIPCHAT['ROOM'],\n",
        "    hipchat_access_token=HIPCHAT['ACCESS_TOKEN'],\n",
        "    input_adapter='chatterbot.input.HipChat',\n",
        "    output_adapter='chatterbot.output.HipChat',\n",
        "    trainer='chatterbot.trainers.ChatterBotCorpusTrainer'\n",
        ")\n",
        "\n",
        "chatbot.train('/content/sample_data/english')\n",
        "\n",
        "# 没错，while True，会一直聊下去！\n",
        "while True:\n",
        "    try:\n",
        "        response = chatbot.get_response(None)\n",
        "\n",
        "    # 直到按ctrl-c 或者 ctrl-d 才会退出\n",
        "    except (KeyboardInterrupt, EOFError, SystemExit):\n",
        "        break"
      ],
      "execution_count": 19,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "s5qknP5484QS",
        "colab_type": "text"
      },
      "source": [
        "#### 邮件回复的聊天系统"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "23u2eWKBHNDW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# -*- coding: utf-8 -*-\n",
        "from chatterbot import ChatBot\n",
        "from settings import MAILGUN\n",
        "\n",
        "'''\n",
        "这个功能需要你新建一个文件settings.py，并在里面写入如下的配置:\n",
        "MAILGUN = {\n",
        "    \"CONSUMER_KEY\": \"my-mailgun-api-key\",\n",
        "    \"API_ENDPOINT\": \"https://api.mailgun.net/v3/my-domain.com/messages\"\n",
        "}\n",
        "'''\n",
        "\n",
        "# 下面这个部分可以改成你自己的邮箱\n",
        "FROM_EMAIL = \"mailgun@salvius.org\"\n",
        "RECIPIENTS = [\"gunthercx@gmail.com\"]\n",
        "\n",
        "bot = ChatBot(\n",
        "    \"Mailgun Example Bot\",\n",
        "    mailgun_from_address=FROM_EMAIL,\n",
        "    mailgun_api_key=MAILGUN[\"CONSUMER_KEY\"],\n",
        "    mailgun_api_endpoint=MAILGUN[\"API_ENDPOINT\"],\n",
        "    mailgun_recipients=RECIPIENTS,\n",
        "    input_adapter=\"chatterbot.input.Mailgun\",\n",
        "    output_adapter=\"chatterbot.output.Mailgun\",\n",
        "    storage_adapter=\"chatterbot.storage.JsonFileStorageAdapter\",\n",
        "    database=\"../database.db\"\n",
        ")\n",
        "\n",
        "# 简单的邮件回复\n",
        "response = bot.get_response(\"How are you?\")\n",
        "print(\"Check your inbox at \", RECIPIENTS)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wx6LiWok89R3",
        "colab_type": "text"
      },
      "source": [
        "#### 一个中文的例子\n",
        "\n",
        "注意chatterbot，中文聊天机器人的场景下一定要用python3.X，用python2.7会有编码问题。"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EAxJDhHsHNBF",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 428
        },
        "outputId": "ec90e536-3dd0-4d53-dc4a-d292cbe18f21"
      },
      "source": [
        "#!/usr/bin/python\n",
        "# -*- coding: utf-8 -*-\n",
        "\n",
        "#手动设置一些语料\n",
        "from chatterbot import ChatBot\n",
        "from chatterbot.trainers import ListTrainer\n",
        " \n",
        " \n",
        "Chinese_bot = ChatBot(\"Training demo\")\n",
        "#Chinese_bot.set_trainer(ListTrainer)\n",
        "Chinese_bot=[\n",
        "    '你好',\n",
        "    '你好',\n",
        "    '有什么能帮你的？',\n",
        "    '想买数据科学的课程',\n",
        "    '具体是数据科学哪块呢？',\n",
        "    '机器学习',\n",
        "]\n",
        " \n",
        "trainer=ListTrainer(chatbot)\n",
        "trainer.train(Chinese_bot)\n",
        "\n",
        "# 测试一下\n",
        "question = '你好'\n",
        "print(question)\n",
        "response = chatbot.get_response(question)\n",
        "print(response)\n",
        "\n",
        "print(\"\\n\")\n",
        "\n",
        "question = '请问哪里能买数据科学的课程'\n",
        "print(question)\n",
        "response = chatbot.get_response(question)\n",
        "print(response)"
      ],
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "List Trainer: [####################] 100%"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "INFO:chatterbot.chatterbot:Beginning search for close text match\n",
            "INFO:chatterbot.chatterbot:Processing search results\n",
            "INFO:chatterbot.chatterbot:Similar text found: 你好 1.0\n",
            "INFO:chatterbot.chatterbot:Using \"你好\" as a close match to \"你好\" with a confidence of 1.0\n",
            "INFO:chatterbot.chatterbot:Selecting response from 2 optimal responses.\n",
            "INFO:chatterbot.response_selection:Selecting first response from list of 2 options.\n",
            "INFO:chatterbot.chatterbot:Response selected. Using \"你好\"\n",
            "INFO:chatterbot.chatterbot:BestMatch selected \"你好\" as a response with a confidence of 1.0\n",
            "INFO:chatterbot.chatterbot:Adding \"你好\" as a response to \"What time is it?\"\n",
            "INFO:chatterbot.chatterbot:Beginning search for close text match\n",
            "INFO:chatterbot.chatterbot:Processing search results\n",
            "INFO:chatterbot.chatterbot:Using \"请问哪里能买数据科学的课程\" as a close match to \"请问哪里能买数据科学的课程\" with a confidence of 0\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "\r\n",
            "你好\n",
            "你好\n",
            "\n",
            "\n",
            "请问哪里能买数据科学的课程\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "INFO:chatterbot.chatterbot:No responses found. Generating alternate response list.\n",
            "INFO:chatterbot.chatterbot:No known response to the input was found. Selecting a random response.\n",
            "INFO:chatterbot.chatterbot:BestMatch selected \"Why would I feel sad? I don't understand.\" as a response with a confidence of 0\n",
            "INFO:chatterbot.chatterbot:Adding \"请问哪里能买数据科学的课程\" as a response to \"你好\"\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Why would I feel sad? I don't understand.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gmEUvglvwk-0",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        },
        "outputId": "6d115fdb-b684-4ba7-f56f-0ed7b3b779c4"
      },
      "source": [
        "!pip install chatterbot"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: chatterbot in /usr/local/lib/python3.6/dist-packages (1.0.8)\n",
            "Requirement already satisfied: pytz in /usr/local/lib/python3.6/dist-packages (from chatterbot) (2018.9)\n",
            "Requirement already satisfied: sqlalchemy<1.4,>=1.3 in /usr/local/lib/python3.6/dist-packages (from chatterbot) (1.3.18)\n",
            "Requirement already satisfied: mathparse<0.2,>=0.1 in /usr/local/lib/python3.6/dist-packages (from chatterbot) (0.1.2)\n",
            "Requirement already satisfied: python-dateutil<2.9,>=2.8 in /usr/local/lib/python3.6/dist-packages (from chatterbot) (2.8.1)\n",
            "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil<2.9,>=2.8->chatterbot) (1.15.0)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yG1SzND69Dcd",
        "colab_type": "text"
      },
      "source": [
        "#### 利用已经提供好的小中文语料库"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fSosBnWFHM_B",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#!/usr/bin/python\n",
        "# -*- coding: utf-8 -*-\n",
        "from chatterbot import ChatBot\n",
        "from chatterbot.trainers import ChatterBotCorpusTrainer\n",
        " \n",
        "chatbot = ChatBot(\"ChineseChatBot\")\n",
        "#chatbot.set_trainer(ChatterBotCorpusTrainer)\n",
        "trainer=ChatterBotCorpusTrainer(chatbot)\n",
        " \n",
        "# 使用中文语料库训练它\n",
        "trainer.train(\"/content/drive/My Drive/VQA/chinese\")\n",
        " \n",
        "# 开始对话\n",
        "while True:\n",
        "    print(chatbot.get_response(input(\">\")))"
      ],
      "execution_count": 51,
      "outputs": []
    }
  ]
}